<script setup lang="ts">
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { saveDataApi } from '@/api/logisticsCompany/index'
import { useEventBus } from '@/hooks/event/useEventBus'
import AddForm from './components/AddForm.vue'

const { emit } = useEventBus()
const { push, go } = useRouter()
const { t } = useI18n()

const formRef = ref()
const loading = ref(false)

// 保存
const save = async () => {
  if (!formRef.value) return

  try {
    loading.value = true
    const formData = await formRef.value.submitForm()
    if (formData) {
      const res = await saveDataApi(formData)
      if (res?.code === 0) {
        emit('getList', 'add')
        push('/logistics/company')
      }
    }
  } catch (error) {
    console.error('Save failed:', error)
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <ContentDetailWrap :title="t('exampleDemo.add')" @back="push('/logistics/company')">
    <AddForm ref="formRef" />

    <template #header>
      <BaseButton @click="go(-1)">
        {{ t('common.back') }}
      </BaseButton>
      <BaseButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </BaseButton>
    </template>
  </ContentDetailWrap>
</template>
